/**
 * 初始化进货单据查询详情对话框
 */
var PurchaseGoodsInfoDlg = {
    id: "PurchaseGoodsTable",	//商品表格id
    table:null,
    purchaseGoodsInfoData : {}
};

/**
 * 清除数据
 */
PurchaseGoodsInfoDlg.clearData = function() {
    this.purchaseGoodsInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
PurchaseGoodsInfoDlg.set = function(key, val) {
    this.purchaseGoodsInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
PurchaseGoodsInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
PurchaseGoodsInfoDlg.close = function() {
    parent.layer.close(window.parent.PurchaseGoods.layerIndex);
}

/**
 * 收集数据
 */
PurchaseGoodsInfoDlg.collectData = function() {
    this
    .set('id')
    .set('goodsCode')
    .set('goodsModel')
    .set('goodsName')
    .set('goodsNum')
    .set('unitPrice')
    .set('totalPrice')
    .set('goodsUnit')
    .set('purchaseId')
    .set('goodsCategoryId')
    .set('createBy')
    .set('createDate')
    .set('updateBy')
    .set('updateDate')
    .set('remarks')
    .set('delFlag');
}

/**
 * 提交添加
 */
PurchaseGoodsInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/purchaseGoods/add", function(data){
        Feng.success("添加成功!");
        window.parent.PurchaseGoods.table.refresh();
        PurchaseGoodsInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.purchaseGoodsInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
PurchaseGoodsInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/purchaseGoods/update", function(data){
        Feng.success("修改成功!");
        window.parent.PurchaseGoods.table.refresh();
        PurchaseGoodsInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.purchaseGoodsInfoData);
    ajax.start();
}


/**
 * 初始化表格的列
 */
PurchaseGoodsInfoDlg.initColumn = function () {
    return [
        {title: '主键', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '商品编码', field: 'goodsCode', visible: true, align: 'center', valign: 'middle'},
        {title: '商品型号', field: 'goodsModel', visible: true, align: 'center', valign: 'middle'},
        {title: '商品名称', field: 'goodsName', visible: true, align: 'center', valign: 'middle'},
        {title: '购进商品数量', field: 'goodsNum', visible: true, align: 'center', valign: 'middle'},
        {title: '采购价', field: 'unitPrice', visible: true, align: 'center', valign: 'middle'},
        {title: '进货总价', field: 'totalPrice', visible: true, align: 'center', valign: 'middle'},
        {title: '商品单位', field: 'goodsUnit', visible: true, align: 'center', valign: 'middle'},
        {title: '清单ID', field: 'purchaseId', visible: true, align: 'center', valign: 'middle'},
        {title: '商品分类ID', field: 'goodsCategoryId', visible: false, align: 'center', valign: 'middle'},
        {title: '创建者', field: 'createBy', visible: false, align: 'center', valign: 'middle'},
        {title: '创建时间', field: 'createDate', visible: true, align: 'center', valign: 'middle'},
        {title: '更新者', field: 'updateBy', visible: false, align: 'center', valign: 'middle'},
        {title: '更新时间', field: 'updateDate', visible: false, align: 'center', valign: 'middle'},
        {title: '备注信息', field: 'remarks', visible: true, align: 'center', valign: 'middle'},
        {title: '删除标记（0：正常；1：删除）', field: 'delFlag', visible: false, align: 'center', valign: 'middle'}
    ];
};

$(function() {
    //商品列表表格
    var defaultColunms = PurchaseGoodsInfoDlg.initColumn();
    var purchaseId = $("#purchaseId").val();
    var table = new BSTable(PurchaseGoodsInfoDlg.id, "/purchaseGoods/detail/"+purchaseId, defaultColunms);
    table.setTableHeight(0);
    table.setShowRefresh(false);
    table.setShowColumns(false);
    table.setPaginationType("client");
    PurchaseGoodsInfoDlg.purchaseGoodsTable = table.init();

});
